package com.cmc.cloud.cmclink.doc.service.impl.so;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.cmc.cloud.cmclink.doc.constants.errorconstant.DgDescErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.DgDescConvert;
import com.cmc.cloud.cmclink.doc.entity.DgDescDO;
import com.cmc.cloud.cmclink.doc.mapper.DgDescMapper;
import com.cmc.cloud.cmclink.doc.service.so.DgDescService;
import com.cmc.cloud.cmclink.doc.vo.sovo.dginfovo.DgDescBaseVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.dginfovo.DgDescCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.dginfovo.DgDescPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.dginfovo.DgDescUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;

/**
 * 危险品信息补充 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class DgDescServiceImpl implements DgDescService {

    @Resource
    private DgDescMapper dgDescMapper;

    @Override
    public void createDgDesc(DgDescCreateReqVO createReqVO) {
        // 检验唯一性
        validateUnique(createReqVO, null);

        // 插入
        DgDescDO dgDesc = DgDescConvert.INSTANCE.convert(createReqVO);
        dgDescMapper.insert(dgDesc);
    }

    private void validateUnique(DgDescBaseVO vo, Long id) {
        LambdaQueryWrapperX<DgDescDO> queryWrapperX = new LambdaQueryWrapperX<DgDescDO>()
            .eqIfPresent(DgDescDO::getUnNo, vo.getUnNo()).eqIfPresent(DgDescDO::getImoClass, vo.getImoClass());
        if (id != null) {
            queryWrapperX.ne(DgDescDO::getId, id);
        }
        if (dgDescMapper.selectCount(queryWrapperX) != 0L) {
            throw ServiceExceptionUtil.exception(DgDescErrorCodeConstants.DG_INFO_LIMIT_EXISTED);
        }
    }

    @Override
    public void updateDgDesc(DgDescUpdateReqVO updateReqVO) {
        // 检验唯一性
        validateUnique(updateReqVO, updateReqVO.getId());

        // 校验存在
        validateDgDescExists(updateReqVO.getId());
        // 更新
        DgDescDO updateObj = DgDescConvert.INSTANCE.convert(updateReqVO);
        dgDescMapper.updateById(updateObj);
    }

    @Override
    public void deleteDgDesc(Long id) {
        // 校验存在
        validateDgDescExists(id);
        // 删除
        dgDescMapper.deleteById(id);
    }

    private void validateDgDescExists(Long id) {
        if (dgDescMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(DgDescErrorCodeConstants.DG_INFO_NOT_EXISTED);
        }
    }

    @Override
    public DgDescDO getDgDesc(Long id) {
        return dgDescMapper.selectById(id);
    }

    @Override
    public List<DgDescDO> getDgDescList(Collection<Long> ids) {
        return dgDescMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DgDescDO> getDgDescPage(DgDescPageReqVO reqVO) {
        LambdaQueryWrapperX<DgDescDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eqIfPresent(DgDescDO::getPortCode, reqVO.getPortCode())
            .eqIfPresent(DgDescDO::getUnNo, reqVO.getUnNo()).likeIfPresent(DgDescDO::getPsnEn, reqVO.getPsnEn())
            .likeIfPresent(DgDescDO::getPsnCn, reqVO.getPsnCn());
        return dgDescMapper.selectPage(reqVO, queryWrapperX);
    }
}
